package com.gy.spring.mvc.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * TODO 用户登陆控制
 *
 * @author geYang
 * @date 2018-02-11
 */
@RestController
@RequestMapping("login")
@Api(description = "用户登录API")
public class LoginController {
    private final String USERNAME = "geYang";
    private final String PASSWORD = "geYang888888";

    @ApiOperation(value = "用户登录", notes = "获取用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密    码", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public Object login(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        if (username == null || !username.equals(USERNAME) || password == null || !password.equals(PASSWORD)) {
            return "FAIL";
        } else {
            Cookie cookie = new Cookie("sso", username + "|" + password);
            cookie.setPath("/");
            response.addCookie(cookie);
            return "SUCCESS";
        }
    }


}
